var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var layer;
var list;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    var menuId = $("#menuId").val();
    if(sessionStorage.getItem(menuId+'add') == 1){
        $("#addConfig").css('display','inline');
    }
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入



        var start = {
            min: laydate.now()
            ,max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            min: laydate.now()
            ,max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };


    });
    setTimeout(function () {
        findSystenConfigList();
    },200);

});



//查询系统配置列表
function findSystenConfigList(){
    var menuId = $("#menuId").val();
    var state = $("#state").val();
    var configName = $("#configName").val();

    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "state":state,
        "configName":configName
    };
    $.ajax({
        type:"post",
        url: "/system/findSystenConfigList",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code == 1 && data.code != undefined){
                list = data.systemGlobalConfigs;
                page = data.page;
                var html = "";
                for(var b in list){
                    var state = "";
                    if(list[b].state == 1){
                        state = "正常";
                    }else {
                        state = "不生效";
                    }
                    html += '<tr><td>'+list[b].configKey+'</td>' +
                        '<td>'+list[b].configName+'</td>' +
                        '<td>'+list[b].configContent+'</td>' +
                        '<td>'+list[b].configMin+'</td>' +
                        '<td>'+list[b].configMax+'</td>'+
                        '<td>'+list[b].configNormal+'</td>'+
                        '<td>'+list[b].configDesc+'</td>'+
                        '<td>'+new Date(list[b].createTime).toLocaleString()+'</td>'+
                        '<td>'+(list[b].modifyTime == null ? "" : new Date(list[b].modifyTime).toLocaleString())+'</td>'+
                        '<td>'+state+'</td><td>';
                    if(sessionStorage.getItem(menuId+'edit') == 1){
                        html += '<button class="layui-btn layui-btn-normal" onclick="showUpdatePage('+b+')">修改</button>';
                    }
                    html += '</td></tr>';

                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findSystenConfigList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
        }

    });
}


//查询按钮
function search() {
    pageNum = 1;
    findSystenConfigList();
}

//打开修改页面
var index;
function showUpdatePage(b){
    var config = list[b];
    var html = '<table style="width: 100%;border-collapse: separate;border-spacing: 10px;margin-top: 10px;margin-bottom:100px;">';
    html += '<tr><td style="width: 30%;text-align: right">配置key</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigKey" value="'+config.configKey+'" readonly style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置名称</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigName" value="'+config.configName+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置内容</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigContent" value="'+config.configContent+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">最大配置</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigMax" value="'+config.configMax+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">最小配置</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigMin" value="'+config.configMin+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置值</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigNormal" value="'+config.configNormal+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置描述</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigDesc" value="'+config.configDesc+'" style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置组</td><td style="text-align: left;padding-left: 10px"><input id="updateConfigGroup" value="'+config.configGroup+'" style="height: 30px;width: 500px"/></td></tr>';
        if(config.state == 1){
            html += '<tr><td style="width: 30%;text-align: right">状态</td><td style="text-align: left;padding-left: 10px"><input name="updateState" type="radio" value="1" style="height: 30px;width: 50px" checked="checked"/>生效<input name="updateState" type="radio" value="0" style="height: 30px;width: 50px"/>不生效</td></tr>';
        }else {
            html += '<tr><td style="width: 30%;text-align: right">状态</td><td style="text-align: left;padding-left: 10px"><input name="updateState" type="radio" value="1" style="height: 30px;width: 50px"/>生效<input name="updateState" type="radio" value="0" style="height: 30px;width: 50px" checked="checked"/>不生效</td></tr>';
        }

         html += '<tr><td style="width: 30%;"><button style="float: right;"class="layui-btn layui-btn-normal" onclick="updateConfig('+config.id+',0,this)">取消保存</button></td><td style="width: 50%;text-align: center"><button class="layui-btn layui-btn-normal" onclick="updateConfig('+config.id+',1,this)">确认保存</button></td></tr>';

    html += '</table>';
    index = layer.open({
        title: ['修改系统配置','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['60%', '600px']
        ,maxmin:true
    });
}

//修改系统配置
function updateConfig(id,btType,obj) {

    if(btType == 0){
        layer.close(index);
        return false;
    }

    $(obj).attr("disabled",true);

    var updateConfigKey = $("#updateConfigKey").val();
    if(updateConfigKey == "" || updateConfigKey == undefined){
        alert("配置key不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigName = $("#updateConfigName").val();
    if(updateConfigName == "" || updateConfigName == undefined){
        alert("配置名称不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigContent = $("#updateConfigContent").val();
    if(updateConfigContent == "" || updateConfigContent == undefined){
        alert("配置内容不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigMax = $("#updateConfigMax").val();
    if(updateConfigMax == "" || updateConfigMax == undefined){
        alert("最大配置不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(updateConfigMax)){
        alert("最大配置填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigMin = $("#updateConfigMin").val();
    if(updateConfigMin == "" || updateConfigMin == undefined){
        alert("最小配置不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(updateConfigMin)){
        alert("最小配置填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigNormal = $("#updateConfigNormal").val();
    if(updateConfigNormal == "" || updateConfigNormal == undefined){
        alert("配置值不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(updateConfigNormal)){
        alert("配置值填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigDesc = $("#updateConfigDesc").val();
    if(updateConfigDesc == "" || updateConfigDesc == undefined){
        alert("配置描述不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateConfigGroup = $("#updateConfigGroup").val();
    if(updateConfigGroup == "" || updateConfigGroup == undefined){
        alert("配置组不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var updateState = $("input[name='updateState']:checked").val();


    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id,
        "configKey":updateConfigKey,
        "configName":updateConfigName,
        "configContent":updateConfigContent,
        "configMax":updateConfigMax,
        "configMin":updateConfigMin,
        "configNormal":updateConfigNormal,
        "configDesc":updateConfigDesc,
        "configGroup":updateConfigGroup,
        "state":updateState
    };
    $.ajax({
        type:"post",
        url:"/system/updateConfig",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("修改成功");
                layer.close(index);
                findSystenConfigList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(index);
            }else if(data.code == 2 && data.code != undefined){
                alert("修改失败");
                findSystenConfigList();
                layer.close(index);
            }else if(data.code == 3 && data.code != undefined){
                alert("修改失败");
                findSystenConfigList();
                layer.close(index);
            }
        }
    });
}

//打开添加系统配置页面
var addIndex;
function showAddConfigPage() {
    var html = '<table style="width: 100%;border-collapse: separate;border-spacing: 10px;margin-top: 10px;margin-bottom:100px;">';
    html += '<tr><td style="width: 30%;text-align: right">配置key</td><td style="text-align: left;padding-left: 10px"><input id="addConfigKey"   style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置名称</td><td style="text-align: left;padding-left: 10px"><input id="addConfigName"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置内容</td><td style="text-align: left;padding-left: 10px"><input id="addConfigContent"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">最大配置</td><td style="text-align: left;padding-left: 10px"><input id="addConfigMax"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">最小配置</td><td style="text-align: left;padding-left: 10px"><input id="addConfigMin"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置值</td><td style="text-align: left;padding-left: 10px"><input id="addConfigNormal"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置描述</td><td style="text-align: left;padding-left: 10px"><input id="addConfigDesc"  style="height: 30px;width: 500px"/></td></tr>'+
        '<tr><td style="width: 30%;text-align: right">配置组</td><td style="text-align: left;padding-left: 10px"><input id="addConfigGroup"  style="height: 30px;width: 500px"/></td></tr>'+

        '<tr><td style="width: 30%;text-align: right">状态</td><td style="text-align: left;padding-left: 10px"><input name="addState" type="radio" value="1" style="height: 30px;width: 50px" checked="checked"/>生效<input name="addState" type="radio" value="0" style="height: 30px;width: 50px"/>不生效</td></tr>';


    html += '<tr><td style="width: 30%;"><button style="float: right;"class="layui-btn layui-btn-normal" onclick="addConfig(0,this)">取消保存</button></td><td style="width: 50%;text-align: center"><button class="layui-btn layui-btn-normal" onclick="addConfig(1,this)">确认保存</button></td></tr>';

    html += '</table>';
    addIndex = layer.open({
        title: ['添加系统配置','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['60%', '600px']
        ,maxmin:true
    });
}

//添加系统配置
function addConfig(btType,obj) {
    if(btType == 0){
        layer.close(addIndex);
        return false;
    }

    $(obj).attr("disabled",true);

    var addConfigKey = $("#addConfigKey").val();
    if(addConfigKey == "" || addConfigKey == undefined){
        alert("配置key不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigName = $("#addConfigName").val();
    if(addConfigName == "" || addConfigName == undefined){
        alert("配置名称不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigContent = $("#addConfigContent").val();
    if(addConfigContent == "" || addConfigContent == undefined){
        alert("配置内容不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigMax = $("#addConfigMax").val();
    if(addConfigMax == "" || addConfigMax == undefined){
        alert("最大配置不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(addConfigMax)){
        alert("最大配置填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigMin = $("#addConfigMin").val();
    if(addConfigMin == "" || addConfigMin == undefined){
        alert("最小配置不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(addConfigMin)){
        alert("最小配置填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigNormal = $("#addConfigNormal").val();
    if(addConfigNormal == "" || addConfigNormal == undefined){
        alert("配置值不能为空");
        $(obj).attr("disabled",false);
        return false;
    }else if(isNaN(addConfigNormal)){
        alert("配置值填写错误");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigDesc = $("#addConfigDesc").val();
    if(addConfigDesc == "" || addConfigDesc == undefined){
        alert("配置描述不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var addConfigGroup = $("#addConfigGroup").val();
    if(addConfigGroup == "" || addConfigGroup == undefined){
        alert("配置组不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var addState = $("input[name='addState']:checked").val();


    var menuId = $("#menuId").val();
    var params = {
        "type":"add",
        "menuId":menuId,
        "configKey":addConfigKey,
        "configName":addConfigName,
        "configContent":addConfigContent,
        "configMax":addConfigMax,
        "configMin":addConfigMin,
        "configNormal":addConfigNormal,
        "configDesc":addConfigDesc,
        "configGroup":addConfigGroup,
        "state":addState
    };
    $.ajax({
        type:"post",
        url:"/system/addConfig",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("添加成功");
                layer.close(addIndex);
                findSystenConfigList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(addIndex);
            }else if(data.code == 2 && data.code != undefined){
                alert("添加失败");
                findSystenConfigList();
                layer.close(addIndex);
            }else if(data.code == 3 && data.code != undefined){
                alert("添加失败");
                findSystenConfigList();
                layer.close(addIndex);
            }
        }
    });
}
